<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * print_object
 *
 * Lets you determine whether an array index is set and whether it has a value.
 * If the element is empty it returns FALSE (or whatever you specify as the default value.)
 *
 * @access	public
 * @param	object
 */
if ( ! function_exists('print_object') )
{
	function print_object($object)
	{
		print("<pre>");
        print_r($object);
        print("</pre>");
	}
}

/**
 * date_create_from_format
 *
 *
 * @access	public
 * @param	datestring in YYYY-MM-DD format
 */
if ( ! function_exists('date_create_from_format'))
{
	function date_create_from_format($format, $datestring) {
		return new DateTime($datestring);
	}
}

/**
 * app_time
 *
 *
 */
if ( ! function_exists('app_time'))
{
	function app_time() {
		return gmt_to_local(now(), "UP3");
	}
}

function my_add_date($datestring = "", $offset = "") {
    if($datestring!="" && $offset!= "") {
        $date = date('Y-m-d', strtotime($datestring.' + '.$offset.' day'));
        return $date;
    } else {
        return $datestring;
    }
}

/**
* Convert timestamp to MySQL's DATE or DATETIME (YYYY-MM-DD hh:mm:ss)
*
* Returns the DATE or DATETIME equivalent of a given timestamp
*
* @author Clemens Kofler <clemens.kofler@chello.at>
* @access    public
* @return    string
*/
function timestamp_to_mysqldatetime($timestamp = "", $datetime = true)
{
  if(empty($timestamp) || !is_numeric($timestamp)) $timestamp = time();

    return ($datetime) ? date("Y-m-d H:i:s", $timestamp) : date("Y-m-d", $timestamp);
}

if ( ! function_exists('format_list'))
{
	function format_list( $result, $id_field, $label_field) {
	   $listresult = array();
        foreach( $result as $resultitem  ) {
            array_push($listresult, array( 'label' => $resultitem[$label_field], 'value' => $resultitem[$id_field] ));
        }
        return $listresult;
	}
}


if ( ! function_exists('in_list'))
{
	function in_list( $value, $list, $key) {
        $is_in_list = false;
        foreach( $list as $item  ) {
            if($item[$key] == $value) {
                $is_in_list = true;
            }
        }
        return $is_in_list;
	}
}

if ( ! function_exists('key_index_list'))
{
	function key_index_list( $list, $key) {
        $key_list = array();
        foreach( $list as $item  ) {
            $key_list[$item[$key]] = $item;
        }
        return $key_list;
	}
}

function convertIntegerToWords($x)
{

$nwords = array(    'Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 
                     'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 
                     'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 
                     'Nineteen', 'Twenty', 30 => 'Thirty', 40 => 'Forty', 
                     50 => 'Fifty', 60 => 'Sixty', 70 => 'Seventy', 80 => 'Eighty', 
                     90 => 'Ninety' );

     if(!is_numeric($x)) 
     { 
         $w = '#'; 
     }else if(fmod($x, 1) != 0) 
     { 
         $w = '#'; 
     }else{ 
         if($x < 0) 
         { 
             $w = 'minus '; 
             $x = -$x; 
         }else{ 
             $w = ''; 
         } 
         if($x < 21) 
         { 
             $w .= $nwords[$x]; 
         }else if($x < 100) 
         { 
             $w .= $nwords[10 * floor($x/10)]; 
             $r = fmod($x, 10); 
             if($r > 0) 
             { 
                 $w .= ' '. $nwords[$r]; 
             } 
         } else if($x < 1000) 
         { 
             $w .= $nwords[floor($x/100)] .' Hundred'; 
             $r = fmod($x, 100); 
             if($r > 0) 
             { 
                 $w .= ' and '. convertIntegerToWords($r); 
             } 
         } else if($x < 1000000) 
         { 
             $w .= convertIntegerToWords(floor($x/1000)) .' Thousand'; 
             $r = fmod($x, 1000); 
             if($r > 0) 
             { 
                 $w .= ' '; 
                 if($r < 100) 
                 { 
                     //$w .= 'and'; 
                 } 
                 $w .= convertIntegerToWords($r); 
             } 
         } else { 
             $w .= convertIntegerToWords(floor($x/1000000)) .' Million'; 
             $r = fmod($x, 1000000); 
             if($r > 0) 
             { 
                 $w .= ' '; 
                 if($r < 100) 
                 { 
                     $word .= 'and '; 
                 } 
                 $w .= convertIntegerToWords($r); 
             } 
         } 
     } 
     return $w; 
}

function CurrencyToWords($number)
{
	if(!is_numeric($number)) return $number;
	$nums = explode('.', $number);
	$out = convertIntegerToWords($nums[0]) . ' Riyals';
	if(isset($nums[1])) {
	$out .= ' and ' . convertIntegerToWords($nums[1]) .' Dirhams';
	}
	return $out;
}

function get_year_term($term_start, $term_end, $year) {
    $term = array();
    $term_label = "";
    $is_current_term = 0;
    $current_month = date("n");
    $current_year = date("Y");
    if($current_month <=3) {  // Fix for Jan & Feb where accademic year is current year - 1.
        $current_year--;
    }
    $term_start_time = mktime(10,0,0,$term_start,1,$year);
    if($term_end < $term_start) {   //Year change Eg. Nov - Feb  ie. 11 - 2
        $term_end_time = mktime(10,0,0,$term_end,1,($year+1));
        $term_label = date("M",$term_start_time)."&#8217; ".date("y",$term_start_time)." to ".date("M",$term_end_time)."&#8217; ".date("y",$term_end_time);
        if( ($current_year==$year) && (($term_start <=  $current_month && $current_month <= 12) || ( 1 <= $current_month && $current_month <= $term_end )) ) {
            $is_current_term = 1;
        }
    } else {
        $term_end_time = mktime(10,0,0,$term_end,1,$year);
        $term_label = date("M",$term_start_time)."&#8217; ".date("y",$term_start_time)." to ".date("M",$term_end_time)."&#8217; ".date("y",$term_end_time);
        if( ($current_year==$year) && $term_start <= $current_month && $current_month <= $term_end ) {
            $is_current_term = 1;
        }
    }
    $term["term_now"] = $is_current_term;
    $term["term_year"] = $year;
    $term["term_label"] = $term_label;
    return $term;
}

function get_current_accademic_year() {
    $current_month = date("n");
    $current_year = date("Y");
    
    if($current_month <=3) {  // Fix for Jan & Feb where accademic year is current year - 1.
        $current_year--;
    }
    
    return $current_year;
}

function get_term_label($term_start, $term_end) {
    $term_start_time = mktime(10,0,0,$term_start);
    $term_end_time = mktime(10,0,0,$term_end);
    $term_label = date("M",$term_start_time).' to '.date("M",$term_end_time);
    return $term_label;
}

function get_date_format($strdate) {
    $datetime = strtotime($strdate);
    $mysqldate = date("d-m-Y", $datetime);
    return $mysqldate;
}

function format_grid_val($function,$params,$dgitem) {
    $colvalue = "";
    if($function != "" && function_exists($function) && $params != "" && is_array($dgitem )) {
        $param_keys = explode("|",$params);
        $param_array = array();
        foreach($param_keys as $key) {
            $param_val = (isset($dgitem[$key])) ? $dgitem[$key] : "";
            if($param_val!="") {
                array_push($param_array,$param_val);
            }
        }
        if(count($param_array) >0) {
            $colvalue = call_user_func_array($function,$param_array);
        }
    }
    
    return $colvalue;
}

function add_dg_col($col,$meta, &$cols) {
    //'reg_no'=>array('label'=>'Reg. No.','type'=>'link','visible'=>true, 'href'=>'/student/viewstudent')
    $result = $meta;
    
    $result['label'] = $meta['label'];
    $result['type'] = $meta['type'];
    $result['visible'] = true;
    
    $enum = array();
    if($result['type'] == 'enum') {
        $result['enum'] = $meta['enum'];
    }
    
    if($result['type'] == 'ref') {
        $result['type'] = "string";
        $col = $meta['ref']['value'];
    }
    //'receipt_term_id'=>array('label'=>'Term','type'=>'format', 'function'=>'get_term_label', 'param'=>'term_start|term_end', 'visible'=>true),
    if($result['type'] == 'format') {
        $result['function'] = $meta['function'];
        $result['param'] = $meta['param'];
    }
    
    $cols[$col] = $result;
}


/* End of file object_helper.php */